package edu.mse.camel.client.uml.collab;

import org.eclipse.ui.IStartup;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import edu.mse.camel.client.eventbus.pubsub.EventManager;
import edu.mse.camel.client.events.EventIDs;
import edu.mse.camel.client.uml.collab.handlers.GenericReceiveEventHandler;
import edu.mse.camel.client.uml.collab.handlers.ResourceUpdateHandler;
import edu.mse.camel.client.uml.collab.loginstate.SessionState;

/**
 * The activator class controls the plug-in life cycle
 * @author darpan
 */
public class CollabPlugin extends AbstractUIPlugin implements IStartup {

	@Override
	public void earlyStartup() {
		// TODO Auto-generated method stub
		
	}

	// The plug-in ID
	public static final String PLUGIN_ID = "edu.mse.camel.client.uml.collab";

	// The shared instance
	private static CollabPlugin plugin;
	
	/**
	 * The constructor
	 */
	public CollabPlugin() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		GenericReceiveEventHandler m = new GenericReceiveEventHandler();
		SessionState.handler = m;
		super.start(context);
		plugin = this;
		
		
		// install handler for the BASELINE_RESOURCE_UPDATE_EVENT
		ResourceUpdateHandler resource = new ResourceUpdateHandler();
		EventManager.getInstance().subscribeTo(EventIDs.BASELINE_RESOURCE_UPDATE_EVENT, resource);
		EventManager.getInstance().subscribeTo(
				EventIDs.NOTIFY_SESSION_PARTICIPANT_CHANGE_EVENT, new SessionState());
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static CollabPlugin getDefault() {
		return plugin;
	}

}
